
import java.util.ArrayList;
import java.util.List;

public class Main {
    public static int[] solution(int n, int[] a) {
        List<Integer> resultList = new ArrayList<>();
        List<Integer> queue = new ArrayList<>();
        for (int num : a) {
            queue.add(num);
        }
        
        while (true) {
            resultList.add(queue.remove(0));
            if (queue.isEmpty()) break;
            
            Integer e = queue.remove(0);
            queue.add(e);
            
            if (queue.size() == 1) {
                resultList.addAll(queue);
                break;
            }
        }
        
        return resultList.stream().mapToInt(i -> i).toArray();
    }

    public static void main(String[] args) {
        System.out.println(java.util.Arrays.equals(
            solution(5, new int[]{5, 3, 2, 1, 4}),
            new int[]{5, 2, 4, 1, 3}));
        System.out.println(java.util.Arrays.equals(
            solution(4, new int[]{4, 1, 3, 2}),
            new int[]{4, 3, 1, 2}));
        System.out.println(java.util.Arrays.equals(
            solution(6, new int[]{1, 2, 3, 4, 5, 6}),
            new int[]{1, 3, 5, 2, 6, 4}));
    }
}
